#include "compfwk/CEntityFactory.h"

#include "compfwk/compfwk.h"
#include "compfwk/CEntity.h"
#include "compfwk/CEntityServer.h"
#include "compfwk/CIdGenerator.h"

namespace compfwk
{
    CEntityFactory::CEntityFactory( const CString& ar_type, const CString& ar_desc ) : 
        m_type( ar_type ),
        m_description( ar_desc ),
        m_v_comp_serv( ),
        m_p_config( 0 )
    {
    }

    void CEntityFactory::AddComponentFactory( IComponentServer* ap_comp_server )
    {
        m_v_comp_serv.push_back( ap_comp_server );
    }

    void CEntityFactory::SetConfiguration( ICompConfigStrategy* ap_config )
    {
        delete m_p_config;
        m_p_config = ap_config;
    }

    IEntity* CEntityFactory::New( void ) const
    {
        // Get a new identifier
        int eid( CIdGenerator::Instance( ).NewId( ) );

        // Create all the components of the entity
        CVector< IComponentServer* >::Ty::const_iterator it( m_v_comp_serv.begin( ) );
        CVector< IComponentServer* >::Ty::const_iterator end( m_v_comp_serv.end( ) );
        for( ; it != end; ++it )
        {
            (*it)->New( eid );
        }

        // Configure the components
        m_p_config->Configure( );

        // Store the entity in the entity server
        IEntity* p_new_ent = new CEntity( eid, GetType( ), GetDescription( ) );
        CEntityServer::Instance( ).AddEntity( p_new_ent );

        return p_new_ent;
    }

    const CString& CEntityFactory::GetType( void ) const
    {
        return m_type;
    }

    const CString& CEntityFactory::GetDescription( void ) const
    {
        return m_description;
    }
}